package com.glureau.aib.shared.equipe;

import com.google.gwt.user.client.rpc.IsSerializable;

public class PionInfo implements IsSerializable {

	private static final String sf_PARAMETERS = "parameters";
	private static final String sf_IA_REF = "iaRef";
	private static final String sf_TYPE = "type";
	private static final String sf_NAME = "name";
	private PionType m_type;
	private String m_name;
	private String m_iaRef;
	private String m_parameters;
	private int m_numero;

	public void setParameters(String parameters) {
		m_parameters = parameters;
	}

	public String getParameters() {
		return m_parameters;
	}

	public void setAiRef(String iaRef) {
		m_iaRef = iaRef;
	}

	public String getAiRef() {
		return m_iaRef;
	}

	public void setName(String name) {
		m_name = name;
	}

	public String getName() {
		return m_name;
	}

	public void setType(PionType type) {
		m_type = type;
	}

	public PionType getType() {
		return m_type;
	}

	public void setNumero(int num) {
		m_numero = num;
	}
	
	public int getNumero() {
		return m_numero;
	}
	
	@Override
	public String toString() {
		return "[" + getClass() + ":{" + encodeParameters(sf_NAME, m_name)
				+ "," + encodeParameters(sf_TYPE, m_type.toString()) + ","
				+ encodeParameters(sf_IA_REF, m_iaRef) + ","
				+ encodeParameters(sf_PARAMETERS, m_parameters) + "}]";
	}

	private String encodeParameters(String name, String value) {
		return "[" + name + "=" + value + "]";
	}

	private String decodeParameters(String parameters, String name) {
		String key = "[" + name + "=";
		int indexOfBegin = parameters.indexOf(key);
		String result = null;
		if (indexOfBegin != -1) {
			int indexOfEnd = parameters.indexOf("]", indexOfBegin);
			result = parameters.substring(indexOfBegin, indexOfEnd);
		}
		return result;
	}

	@SuppressWarnings("unused")
	private void fromString(String parameters) {
		m_name = decodeParameters(parameters, sf_NAME);
		m_type = PionType.valueOf(decodeParameters(parameters, sf_TYPE));
		m_iaRef = decodeParameters(parameters, sf_IA_REF);
		m_parameters = decodeParameters(parameters, sf_PARAMETERS);
	}

}
